document.addEventListener('DOMContentLoaded', function () {
    // 检查用户是否已登录
    checkAuthStatus();

    // 登录表单提交
    setupFormSubmitEvent('login-form', handleLogin);

    // 注册表单提交
    setupFormSubmitEvent('register-form', handleRegister);

    // 退出登录按钮
    const logoutBtn = document.getElementById('logout-btn');
    if (logoutBtn) {
        logoutBtn.addEventListener('click', handleLogout);
    }
});

// 检查认证状态
function checkAuthStatus() {
    const user = localStorage.getItem('currentUser');
    const guestMenu = document.getElementById('guest-menu');
    const userMenu = document.getElementById('user-menu');
    const usernameDisplay = document.getElementById('username-display');

    if (user && guestMenu && userMenu) {
        // 用户已登录
        toggleMenuVisibility(guestMenu, false);
        toggleMenuVisibility(userMenu, true);

        if (usernameDisplay) {
            const userData = JSON.parse(user);
            usernameDisplay.textContent = userData.username || '用户';
        }
    } else if (guestMenu && userMenu) {
        // 用户未登录
        toggleMenuVisibility(guestMenu, true);
        toggleMenuVisibility(userMenu, false);
    }
}

// 处理登录
function handleLogin() {
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;

    // 简单验证
    if (!username || !password) {
        showNotification('请填写用户名和密码', 'error');
        return;
    }

    // 模拟登录请求
    simulateRequest(1000, () => {
        // 模拟成功登录
        const userData = {
            id: 1,
            username: username,
            email: '',
            avatar: 'https://picsum.photos/seed/user1/100/100'
        };

        localStorage.setItem('currentUser', JSON.stringify(userData));
        showNotification('登录成功，欢迎回来！', 'success');

        // 更新UI
        checkAuthStatus();

        // 重定向到首页或之前的页面
        redirectToPage('index.html', 1500);
    });
}

// 处理注册
function handleRegister() {
    const username = document.getElementById('reg-username').value;
    const phone = document.getElementById('reg-phone').value;
    const email = document.getElementById('reg-email').value;
    const password = document.getElementById('reg-password').value;
    const confirmPassword = document.getElementById('reg-confirm-password').value;

    // 简单验证
    if (!username || !email || !password) {
        showNotification('请填写所有必填字段', 'error');
        return;
    }

    if (password !== confirmPassword) {
        showNotification('两次输入的密码不一致', 'error');
        return;
    }

    // 模拟注册请求
    simulateRequest(1500, () => {
        // 模拟成功注册
        const userData = {
            id: Date.now(),
            username: username,
            email: email,
            avatar: 'https://picsum.photos/seed/user' + Date.now() + '/100/100'
        };

        localStorage.setItem('currentUser', JSON.stringify(userData));
        showNotification('注册成功！已自动登录', 'success');

        // 更新UI
        checkAuthStatus();

        // 重定向到首页或之前的页面
        redirectToPage('index.html', 1500);
    });
}

// 处理退出登录
function handleLogout() {
    localStorage.removeItem('currentUser');
    showNotification('已成功退出登录', 'info');

    // 更新UI
    checkAuthStatus();

    // 重定向到登录页
    redirectToPage('login.html', 1000);
}

// 设置表单提交事件
function setupFormSubmitEvent(formId, handler) {
    const form = document.getElementById(formId);
    if (form) {
        form.addEventListener('submit', function (e) {
            e.preventDefault();
            handler();
        });
    }
}

// 切换菜单可见性
function toggleMenuVisibility(menu, visible) {
    if (visible) {
        menu.classList.remove('hidden');
    } else {
        menu.classList.add('hidden');
    }
}

// 模拟请求
function simulateRequest(delay, callback) {
    setTimeout(callback, delay);
}

// 重定向到指定页面
function redirectToPage(url, delay) {
    setTimeout(() => {
        window.location.href = url;
    }, delay);
}

// 显示通知
function showNotification(message, type) {
    // 简单示例：使用 alert 显示通知
    alert(message);
}